<?php
include_once $_SERVER ['DOCUMENT_ROOT'] . '/ad/sys/com/control/BaseService.php';
include_once $_SERVER ['DOCUMENT_ROOT'] . '/ad/sys/com/model/db/publish/PublishPayToUserDAO.php';
include_once $_SERVER ['DOCUMENT_ROOT'] . '/ad/sys/com/model/util/Util.php';  

class PayToUserService extends BaseService {
	
	public function __construct(){
		parent::__construct();
	 	parent::setTarget($this);	
	 	$this->log = Plog::factory(__FILE__);	
	}
	


	public function create(){
			//		echo $_POST['row'];
		if (get_magic_quotes_gpc ()) {
			$d = stripslashes ( $_POST ['row'] );
		} else {
			$d = $_POST ['row'];
		}
		$records = json_decode ( $d, true );
		$managerAccount=@$_GET["managerAccount"];
		$publishPayToUserDAO=new PublishPayToUserDAO();
//		echo "record=$records";
		if (isset ( $records ['PayId'] )) { //single record
			$records['Account']=$managerAccount;
			$publishPayToUserDAO->insertOrUpdate($records);
		} else { //multiple records
			foreach ( $records as $idx => $record ) {
				$record['Account']=$managerAccount;
				$publishPayToUserDAO->insertOrUpdate($record);
			}
		}
		
		echo '{ success:true, msg:"已儲存紀錄" }';
	}

	/**
	 * 
	 * 取得該會員所有帳務資料
	 */
	public function read(){
		@session_start();
		$managerAccount=@$_SESSION["settleAccount"];
		$start=@$_REQUEST["start"];
		$limit=@$_REQUEST["limit"];
		$settleDAO = new PublishPayToUserDAO();
		$result=$settleDAO->queryAllDateByAccount($managerAccount,$start,$limit);
//		echo $result;
		if ($result[0]->{"Account"}== null) {
			echo "{row:[]}";	
		} else {
			echo "{row:".Util::toJson($result)."}";	
			
		}
	}
	
	//OPERATION: UPDATE
	public function update(){
		if (get_magic_quotes_gpc ()) {
			$d = stripslashes ( $_POST ['row'] );
		} else {
			$d = $_POST ['row'];
		}
		$records = isset ( $d ) ? $d : '';
		$managerAccount=@$_GET["managerAccount"];
		$records = json_decode ( $records, true );
		$publishPayToUserDAO = new PublishPayToUserDAO();
		//update records
		if (isset ( $records ['id'] )) { //single record
			$publishPayToUserDAO->update($managerAccount,$records['PayId'],$records['PayDate'],$records['PayMoney'],$records['Status']);
		} else {
			foreach ( $records as $key => $newRec ) {
				$publishPayToUserDAO->update($managerAccount,$newRec['PayId'],$newRec['PayDate'],$newRec['PayMoney'],$newRec['Status']);
			}
		}
	}
	public function destroy(){
		if (get_magic_quotes_gpc ()) {
			$d = stripslashes ( $_POST ['row'] );
		} else {
			$d = $_POST ['row'];
		}
		$records = json_decode ( $d, true );
		$managerAccount=@$_GET["managerAccount"];
		$publishPayToUserDAO=new PublishPayToUserDAO();
//		print_f($records);
		if (isset ( $records ['PayId'] )) { //single record
			$records['Account']=$managerAccount;
			$publishPayToUserDAO->insertData($rec);
		} else { //multiple records
			foreach ( $records as $idx => $record ) {
				$record[$idx]['Account']=$managerAccount;
				$publishPayToUserDAO->insertData($records [$idx]);
			}
		}
		
		
	}
}
$service =  new PayToUserService();
$service->__call($_GET["action"], null);
//$service->queryTargetAccountMember();
?>